<?php
//---------------------------------------------------------------------------------------
// Author  : BugTraker
// File    : create_db.php - create database via php script.
// Date    : 09/01/2010 00:55
// Website : http://www.bugtraker.pl 
//
// SAC (Simple Add Comment) - Copyright (C) 2009 BugTraker (http://www.bugtraker.pl).
//
// SAC is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// BeFree is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BeFree; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//---------------------------------------------------------------------------------------

require('config.php');

sac_create_db_and_table();


//---------------------------------------------------------------------------------------
// Create db and table for SAC.
//
// Arguments:
// N/A
// 
// Return:
// N/A
//---------------------------------------------------------------------------------------
function sac_create_db_and_table()
{
  $connection = @mysql_connect(SAC_SERVER, SAC_LOGIN, SAC_PASS);
  
  if ($connection)
    {
      $query = sprintf("CREATE DATABASE IF NOT EXISTS %s", SAC_DBASE);                    // Form query to create db.
      $create_db = mysql_query($query);
      if ($create_db)                                                                     // Check if DB has been created.
        {
          $db_connection = @mysql_select_db(SAC_DBASE, $connection) or die($query);       // Connect to created DB.
          $query = "CREATE TABLE `comments` (
                      `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
                      `user_name` VARCHAR(500) NOT NULL,
                      `cmt` VARCHAR (500) NOT NULL,
                      `date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP
                    ) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci";
          $result = mysql_query($query);                                                  // Create table.
          if ($result)                                                                    // Check results.
            {
              printf("DATABASE %s and TABLE comments created.", SAC_DBASE);               // All ok.
            }
          else
            {
              echo "Creating table error: ".mysql_error();                                // Table not created.
            }
        }
      else
        {
          echo "Database not created: ".myslq_error();                                    // Database not created.
        }
    }
  mysql_close();
}

php?>
